package com.man.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.man.entity.Friends;
import com.man.entity.User;

public class FriendsDao {
	static String URL="jdbc:mysql://localhost:3306/manmanage?user=root&password=root";
	public Boolean insert(Friends friends)
	{
		String sql="insert into friends  values(?,?,?,?,?,?,?) ";
		boolean b=false;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, friends.getUserName());
			statement.setString(2, friends.getName());
			statement.setString(3, friends.getPhone());
			statement.setString(4, friends.getEmail());
			statement.setString(5, friends.getWorkplace());
			statement.setString(6,friends.getPlace());
			statement.setString(7,friends.getQQ());
		    statement.execute();
		    b=true;	
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return b;	
	}
	
	public List<Friends> Select(Friends friends) 
	{	List<Friends> list= new ArrayList<Friends>();
		String sql="select * from friends where userName=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,friends.getUserName());
			ResultSet set=	statement.executeQuery();
			while(set.next())
			{	Friends friends2=new Friends();
				friends2.setUserName(set.getString("userName"));
				friends2.setName(set.getString("name"));
				friends2.setQQ(set.getString("QQ"));
				friends2.setPhone(set.getString("phone"));
				friends2.setWorkplace(set.getString("workplace"));
				friends2.setPlace(set.getString("place"));
				friends2.setEmail(set.getString("email"));
				list.add(friends2);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	public Friends oneSelect(Friends friends) 
	{	Friends friends2= new  Friends();
		String sql="select * from friends where userName=? and  name=? ";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,friends.getUserName());
			statement.setString(2, friends.getName());
			ResultSet set=	statement.executeQuery();
			while(set.next())
			{
				friends2.setUserName(set.getString("userName"));
				friends2.setName(set.getString("name"));
				friends2.setQQ(set.getString("QQ"));
				friends2.setPhone(set.getString("phone"));
				friends2.setWorkplace(set.getString("workplace"));
				friends2.setPlace(set.getString("place"));
				friends2.setEmail(set.getString("email"));
				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return friends2;	
	}
	public Boolean delect(Friends friends)
	{
		Boolean b=false;
		String sql="delete from friends where userName=? and  name=? ";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,friends.getUserName());
			statement.setString(2, friends.getName());
			statement.execute();
			b=true;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;	
		
	}

	public Boolean updata(Friends friends) {
		// TODO Auto-generated method stub
		
		Boolean b=false;
		String sql="update friends set phone=?,email=?,workplace=?,place=?,QQ=? where userName=? and name=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, friends.getPhone());
			statement.setString(2, friends.getEmail());
			statement.setString(3, friends.getWorkplace());
			statement.setString(4, friends.getPlace());
			statement.setString(5, friends.getQQ());
			statement.setString(6, friends.getUserName());
			statement.setString(7, friends.getName());	
			statement.execute();
			b=true;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}
}
